package top.hkyzf.study.leetcode.string;

import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 朱峰
 * @date 2022-2-15 11:31
 */
public class 字符串中的第一个唯一字符_387 {
    public int firstUniqChar(String s) {
        Map<Character, Integer> maps = new HashMap<>();
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            maps.put(ch, maps.getOrDefault(ch, 0) + 1);
        }
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            if (maps.get(ch) == 1) {
                return i;
            }
        }
        return -1;
    }

    @Test
    public void testFirstUniqChar () {
        String s = "leetcode";
        System.out.println(firstUniqChar(s));
    }
}
